package com.simtop.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.User;
import com.simtop.pojo.wddata.LayOut;
import com.simtop.pojo.wddata.LayOutVos;
import com.simtop.pojo.wddata.MainTain;
import com.simtop.service.ApiService;
import com.simtop.service.LayOutService;
import com.simtop.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/layout")
@CrossOrigin
@Component
public class LayOutController {
    @Autowired
    private ApiService apiService;
    @Autowired
    private LayOutService layOutService;

    @PostMapping("setlayoutuseStatus")
    public ServerResponse setlayoutuseStatus(@RequestBody String data, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }


        try {

            //先将所有状态变为0
            layOutService.setAllStatus();
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer id = jsonObject.getInteger("id");
            Integer status = jsonObject.getInteger("status");
            layOutService.setlayoutuseStatus(id, status);


            return ServerResponse.createBySuccess();


        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }

    }

    //获取维护
    @PostMapping("/findAllLayOut")
    public ServerResponse findAllLayOut(@RequestBody String data, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }


        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer pageNum = jsonObject.getInteger("pageNum");
            Integer pageSize = jsonObject.getInteger("pageSize");
            if (StringUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (StringUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            PageHelper.startPage(pageNum, pageSize);
            String layoutName = jsonObject.getString("layoutName");
            List<LayOutVos> findAllLayOut = layOutService.findAllLayOut(layoutName);
            PageInfo<LayOutVos> pageInfo = new PageInfo<LayOutVos>(findAllLayOut);


            return ServerResponse.createBySuccess("获取内容成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }


    //获取名称
    @PostMapping("/findAllLayName")
    public ServerResponse findAllLayName(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }


        try {

            List<String> as = layOutService.findAllLayName();

            return ServerResponse.createBySuccess(as);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }


    //改变布局删除状态

    @PostMapping("/changeDeleteStatus")
    public ServerResponse changeDeleteStatus(@RequestBody String data, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }


        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer id = jsonObject.getInteger("id");


            layOutService.changeDeleteStatus(id);

            return ServerResponse.createBySuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }


    @PostMapping("insertplantName")
    public ServerResponse insertplantName(@RequestBody String data, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }


        try {

            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer id = jsonObject.getInteger("id");
            String layoutName = jsonObject.getString("layoutName");
            String layoutPlant = jsonObject.getString("layoutPlant");

            layOutService.insertplantName(id, layoutName,layoutPlant);


            return ServerResponse.createBySuccess();


        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }

    }



    @PostMapping("newinsertplantName")
    public ServerResponse newinsertplantName(@RequestBody String data, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }


        try {
            Integer roleId = u.getRoleId();
            String  rolename = apiService.findRoleName(roleId);

            JSONObject jsonObject = JSONObject.parseObject(data);
            String layoutName = jsonObject.getString("layoutName");
            String layoutPlant = jsonObject.getString("layoutPlant");
            LayOut layOut = new LayOut();
            layOut.setLayoutName(layoutName);
            layOut.setLayoutPlant(layoutPlant);
            layOut.setLayoutTime(new Date());
            layOut.setLayoutUser(rolename);
            layOut.setLayoutuseTime(1);
            layOut.setLayoutuseStatus(0);
            layOut.setEditStatus(0);
            layOut.setStartStatus(0);
            layOut.setDelectStatus(0);



            layOutService.newinsertplantName(layOut);


            return ServerResponse.createBySuccess();


        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }

    }










    @PostMapping("/startU3d")
    @ResponseBody
    public ServerResponse startU3d(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }

        //先将所有启动状态变为 0
        layOutService.getAllOne();

        //改变唯一启动值为1
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer id = jsonObject.getInteger("id");
        Integer status = jsonObject.getInteger("status");
        layOutService.startU3d(id,status);


        return ServerResponse.createBySuccess();



    }
}
